package com.htg.tdtwebapi;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class TdtApiGeocoder extends TdtWebApi{
    public static final String url = "http://api.tianditu.gov.cn/geocoder";

    public TdtApiGeocoder(String key) {
        super(key);
    }

    /**
     * 是指由结构化地址数据（如：北京市海淀区莲花池西路28号）转换为对应坐标点（经纬度）功能，地址解析仅限于国内。
     * @param keyWord
     * @return
     * @throws Exception
     */
    public String geocoder(String keyWord) throws Exception {
        //
        boolean legal = isParamsLegal(keyWord);
        if (!legal)
            throw new Exception("keyWord 为必填参数 不能为null 也不能为空串");
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("keyWord", keyWord);
        params.put("ds", jsonObject.toString());
        params.put("tk", key);
        String response = get(url, params);
        return response;
    }


    /**
     * 是指由结构化地址数据（如：北京市海淀区莲花池西路28号）转换为对应坐标点（经纬度）功能，地址解析仅限于国内。
     * @param keyWord
     * @return
     * @throws Exception
     */
    public Location geocoder2(String keyWord) throws Exception {
        //
        String response = geocoder(keyWord);
        JSONObject jsonObject = new JSONObject(response);
        String status = jsonObject.getString("status");
        if (status.equals("0")){
            JSONObject location = jsonObject.getJSONObject("location");
            Gson gson = new Gson();
            return gson.fromJson(location.toString(), Location.class);
        }else{
            String msg = jsonObject.getString("msg");
            throw new Exception("地理编码异常 ： "+msg);
        }

    }


    /**
     * 将坐标点（经纬度）转换为结构化的地址信息
     * @param lon
     * @param lat
     * @param ver
     * @return
     * @throws Exception
     */
    public String reGeocoder(String lon,String lat,String ver) throws Exception {
        //
        boolean legal = isParamsLegal(lon,lat);
        if (!legal)
            throw new Exception("lon,lat 为必填参数 不能为null 也不能为空串");
        //
        if (ver==null||ver.isEmpty())
            ver = "1";
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lon", lon);
        jsonObject.put("lat", lat);
        jsonObject.put("ver", ver);
        params.put("postStr", jsonObject.toString());
        params.put("tk", key);
        params.put("type","geocode");
        String response = get(url, params);
        return response;
    }



    /**
     * 将坐标点（经纬度）转换为结构化的地址信息
     * @param lon
     * @param lat
     * @param ver
     * @return
     * @throws Exception
     */
    public AddressComponent reGeocoder2(String lon,String lat,String ver) throws Exception {
        //
        String response = reGeocoder(lon, lat, ver);
        JSONObject jsonObject = new JSONObject(response);
        String status = jsonObject.getString("status");
        if (status.equals("0")){
            JSONObject addressComponent = jsonObject.getJSONObject("result").getJSONObject("addressComponent");
            Gson gson = new Gson();
            return gson.fromJson(addressComponent.toString(), AddressComponent.class);
        }else{
            String msg = jsonObject.getString("msg");
            throw new Exception("逆向地理编码异常 ： "+msg);
        }
    }



    public class AddressComponent implements Serializable{
        private String address;
        private String town;
        private String nation;
        private String city;
        private String county_code;
        private String poi_position;
        private String county;
        private String city_code;
        private String address_position;
        private String poi;
        private String province_code;
        private String town_code;
        private String province;
        private String road;
        private int road_distance;
        private int address_distance;
        private int poi_distance;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getTown() {
            return town;
        }

        public void setTown(String town) {
            this.town = town;
        }

        public String getNation() {
            return nation;
        }

        public void setNation(String nation) {
            this.nation = nation;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCounty_code() {
            return county_code;
        }

        public void setCounty_code(String county_code) {
            this.county_code = county_code;
        }

        public String getPoi_position() {
            return poi_position;
        }

        public void setPoi_position(String poi_position) {
            this.poi_position = poi_position;
        }

        public String getCounty() {
            return county;
        }

        public void setCounty(String county) {
            this.county = county;
        }

        public String getCity_code() {
            return city_code;
        }

        public void setCity_code(String city_code) {
            this.city_code = city_code;
        }

        public String getAddress_position() {
            return address_position;
        }

        public void setAddress_position(String address_position) {
            this.address_position = address_position;
        }

        public String getPoi() {
            return poi;
        }

        public void setPoi(String poi) {
            this.poi = poi;
        }

        public String getProvince_code() {
            return province_code;
        }

        public void setProvince_code(String province_code) {
            this.province_code = province_code;
        }

        public String getTown_code() {
            return town_code;
        }

        public void setTown_code(String town_code) {
            this.town_code = town_code;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getRoad() {
            return road;
        }

        public void setRoad(String road) {
            this.road = road;
        }

        public int getRoad_distance() {
            return road_distance;
        }

        public void setRoad_distance(int road_distance) {
            this.road_distance = road_distance;
        }

        public int getAddress_distance() {
            return address_distance;
        }

        public void setAddress_distance(int address_distance) {
            this.address_distance = address_distance;
        }

        public int getPoi_distance() {
            return poi_distance;
        }

        public void setPoi_distance(int poi_distance) {
            this.poi_distance = poi_distance;
        }
    }



    public class Location implements Serializable {
        private int score;
        private String level;
        private String lon;
        private String lat;
        private String keyWord;

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public String getLon() {
            return lon;
        }

        public void setLon(String lon) {
            this.lon = lon;
        }

        public String getLat() {
            return lat;
        }

        public void setLat(String lat) {
            this.lat = lat;
        }

        public String getKeyWord() {
            return keyWord;
        }

        public void setKeyWord(String keyWord) {
            this.keyWord = keyWord;
        }
    }

}
